//
//  WishListViewController.m
//  Memo
//
//  Created by A-01 on 2020/7/8.
//  Copyright © 2020 A-01. All rights reserved.
//

#import "WishListViewController.h"
#import "WishListTableViewCell.h"
#import "AddWishViewController.h"

@interface WishListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UILabel * wishLabel;
@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray * dataArray;
@property (nonatomic, weak)UILabel * noDataLab;

@end

@implementation WishListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    switch (self.type) {
        case 1:
            self.titleLabel.text = @"Wish List";
            break;
        case 2:
            self.titleLabel.text = @"Processing List";
            break;
        case 3:
            self.titleLabel.text = @"Completed List";
            break;
        default:
            break;
    }
    
    [self setupUI];
}


- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:YES];
    
    NSArray * arr = DEF_GET_OBJECT(@"data");
    NSMutableArray * conArr = [NSMutableArray array];
    switch (self.type) {
        case 1:
            self.dataArray = arr;
            self.wishLabel.text = [NSString stringWithFormat:@"Wish(%ld)",arr.count];
            break;
        case 2:
            for (NSDictionary * dic in arr) {
                
                if ([dic[@"state"] isEqualToString:@"processing"]) {
                    
                    [conArr addObject:dic];
                }
            }
            self.dataArray = conArr.copy;
            self.wishLabel.text = [NSString stringWithFormat:@"Wish(%ld)",conArr.count];
            break;
        case 3:
            for (NSDictionary * dic in arr) {
                
                if ([dic[@"state"] isEqualToString:@"completed"]) {
                    
                    [conArr addObject:dic];
                }
            }
            self.dataArray = conArr.copy;
            self.wishLabel.text = [NSString stringWithFormat:@"Wish(%ld)",conArr.count];
            break;
        default:
            break;
    }
    
    
    
    [self.tableView reloadData];
}


- (void)setupUI{
    
    UILabel * wishLabel  = [[UILabel alloc] initWithFrame:CGRectMake(20, kNavHeight + 10, kScreenW - 20, 25)];

    wishLabel.text = @"Wish";
    wishLabel.textColor = [UIColor colorWithHexString:@"F39800"];
    wishLabel.font = [UIFont boldSystemFontOfSize:20];
    
    self.wishLabel = wishLabel;
       
    [self.view addSubview:self.wishLabel];
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.wishLabel.bottom , kScreenW, self.view.height - self.wishLabel.bottom) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor clearColor];
    tableView.rowHeight = 75;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    self.tableView = tableView;
       
    [self.tableView registerClass:[WishListTableViewCell class] forCellReuseIdentifier:@"WishListTableViewCell"];
       
    [self.view addSubview:self.tableView];
    
    
    UILabel * noLab = [UILabel new];
    
    noLab.text = @"No Wish";
    noLab.textColor = ZRWhiteColor;
    noLab.font = [UIFont fontWithName:@"Marker Felt" size:75];
    noLab.textAlignment = NSTextAlignmentCenter;
    noLab.hidden = YES;
    
    self.noDataLab = noLab;
    
    [self.view addSubview:self.noDataLab];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary * dict = self.dataArray[indexPath.row];
    
    AddWishViewController * addVC = [AddWishViewController new];
    
    addVC.titleStr = @"detail";
    addVC.dict = [NSMutableDictionary dictionaryWithDictionary:dict];
    
    [self.navigationController pushViewController:addVC animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.dataArray.count == 0) {
        
        self.noDataLab.hidden = NO;
    }else{
        
        self.noDataLab.hidden = YES;
    }
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WishListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WishListTableViewCell" forIndexPath:indexPath];
    
    NSDictionary * dict = self.dataArray[indexPath.row];
    
    cell.titleLabel.text = dict[@"name"];
    if ([dict[@"state"] isEqualToString:@"completed"] || [dict[@"state"] isEqualToString:@"processing"]) {
        
        cell.statusImageView.image = [UIImage imageNamed:@"processing"];
    }else if ([dict[@"state"] isEqualToString:@"not yet started"]){
        
        cell.statusImageView.image = [UIImage imageNamed:@"notstarted"];
    }else if ([dict[@"state"] isEqualToString:@"expired"]){
        
        cell.statusImageView.image = [UIImage imageNamed:@"expired"];
    }else if ([dict[@"state"] isEqualToString:@"unlimited,processing"]){
        
        cell.statusImageView.image = [UIImage imageNamed:@"unlimited"];
    }else if ([dict[@"state"] isEqualToString:@"unlimited,not yet started"]){
        
        cell.statusImageView.image = [UIImage imageNamed:@"notunlimited"];
    }

    return cell;
}


- (void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    
    [self.noDataLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view).offset(-20);
    }];
}


- (NSArray *)dataArray{
    
    if (!_dataArray) {
        
        _dataArray = [NSArray array];
        
    }
    
    return _dataArray;
}


@end
